package nodes.command;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
import parser.ParserException;
import model.Model;
import nodes.AbstractNode;
import nodes.CloseParensNode;
import nodes.ConstantNode;
import nodes.OpenParensNode;


/**
 * Runs a command(s) if an expression is not 0
 */
public class TellC extends AbstractCommand
{
    public AbstractNode evaluate (List<AbstractNode> myArguments, Model model)
    {
        if (myArguments.size() < Integer.parseInt(resources.getString("tell")))
        {
            throw ParserException.BAD_ARGUMENTS;
        }

        int numberOfTurtles = 0;
        ArrayList<Integer> turtlesToActivate = new ArrayList<Integer>();
        for(AbstractNode currentArgument : myArguments)
        {
            turtlesToActivate.add(currentArgument.getValue());
            numberOfTurtles++;
        }
        model.setActiveTurtles(turtlesToActivate);
        return new ConstantNode(numberOfTurtles);
    }
    
    @Override
    public List<AbstractNode> setUpCommand (Queue<AbstractNode> outputQueue, String name)
    {
        List<AbstractNode> arguments = new ArrayList<AbstractNode>();
        try
        {

            while (!(outputQueue.peek() instanceof CloseParensNode)){
                arguments.add(outputQueue.poll().buildTree(outputQueue));
            }
            outputQueue.poll();
        }
        catch (NullPointerException e)
        {
            throw ParserException.BAD_TOKEN;
        }

        return arguments;
    }
}
